package com.lagou;

import com.lagou.service.Method;
import org.apache.dubbo.rpc.Filter;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.time.Instant;
import java.util.concurrent.*;


public class ConsumerMain {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
        context.start();
        // 拿到接口
        Method method = context.getBean("Method", Method.class);
        // 使用线程池去执行调用任务
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 起始时间
        long startMills = Instant.now().toEpochMilli();
        //  给线程池提交任务
        for (int i = 0; i < 2500; i++) {

            executorService.execute(()->{
                try {
                    TimeUnit.MILLISECONDS.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //  随机访问方法
                final int flag = ThreadLocalRandom.current().nextInt(3);
                String result = null;
                switch (flag) {
                    case 0:
                        result = method.methodA("A");
                        break;
                    case 1:
                        result = method.methodB("B");
                        break;
                    case 2:
                        result = method.methodC("C");
                        break;
                    default:
                        break;

                }
                // 调用结果
                System.out.println("result = " + result);
            });
            System.out.println(i);
        }
        executorService.shutdown();
        // 终止时间
        long endMills = Instant.now().toEpochMilli();
        System.out.println("-----总耗时" + (endMills - startMills) + "-----");


    }

}
